package com.baturu.vin.dffx.service.impl;

import com.baturu.vin.base.service.EPCCacheRealTimeProcess;
import com.baturu.vin.dffx.dal.dao.DffxVinDAO;
import com.baturu.vin.dffx.dto.DffxVinDTO;
import com.baturu.vin.dffx.dto.ResponseCarDTO;
import com.google.common.base.Splitter;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.regex.Pattern;

/**
 * @Author: chenjiahao
 * @Time: 2018/8/31
 */

@Slf4j
@Service("dffxEPCCacheRealTimeProcess")
public class DffxEPCCacheRealTimeProcess implements EPCCacheRealTimeProcess {
    @Autowired
    DffxVinDAO dffxVinDAO;

    @Override
    public boolean buildVinAnalyticalResults(String vinCode, String analyticalResultText) throws Exception {
        DffxVinDTO dffxVinDTO = buildQueryCarResult(vinCode, analyticalResultText);
        if (!Objects.isNull(dffxVinDTO)) {
            saveVinModel(dffxVinDTO, vinCode);
        }
        return true;
    }

    private void saveVinModel(DffxVinDTO dffxVinDTO, String vinCode) {
        DffxVinDTO vinModel = dffxVinDAO.findVinModel(dffxVinDTO.getVinCode());
        if (Objects.isNull(vinModel)) {
            List<DffxVinDTO> list = dffxVinDAO.findCarType(dffxVinDTO.getModelCode());
            if (CollectionUtils.isEmpty(list)) {
                return;
            }
            dffxVinDTO = list.get(0);
            dffxVinDTO.setVinCode(vinCode);
            dffxVinDTO.setBtrBrand("东风风行");
            dffxVinDTO.setBtrSubBrand("东风风行");
            String modelDes = dffxVinDTO.getModelDes();
            if (StringUtils.isNotBlank(modelDes)) {
                setDesInfo(modelDes, dffxVinDTO);
            }

            dffxVinDAO.insert(dffxVinDTO);
        }
    }

    private DffxVinDTO buildQueryCarResult(String vinCode, String analyticalResultText) {
        DffxVinDTO vininfo = null;
        try {
            if (StringUtils.isBlank(analyticalResultText)) {
                return null;
            }
            Gson gson = new Gson();
            ResponseCarDTO responseCarDTO = gson.fromJson(analyticalResultText, ResponseCarDTO.class);
            if (Objects.isNull(responseCarDTO)) {
                return null;
            }
            if (StringUtils.equals(responseCarDTO.getStatus(), "ok")) {
                vininfo = responseCarDTO.getVininfo();

            }

        } catch (Exception e) {
            log.error("vin{} 查询结果转换异常{}", vinCode, e);
        }
        return vininfo;
    }

    private void setDesInfo(String modelDes, DffxVinDTO vininfo) {
        List<String> desList = Splitter.on(" ").splitToList(modelDes);
        if (CollectionUtils.size(desList) == 1) {
            vininfo.setTransmissionType("电动");
            vininfo.setDisplacement("");
            List<String> carType = Splitter.on("版").splitToList(modelDes);
            if (CollectionUtils.size(carType) > 1) {
                vininfo.setCarType(carType.get(1));
            }
        } else {
            desList.forEach(d -> {
                Pattern disRex = Pattern.compile("^\\d\\.\\d.$");
                Pattern disStandardRex = Pattern.compile("^国[\\s\\S]*$");
                Pattern tranTypeRex = Pattern.compile("^[\\s\\S]动$");
                Pattern tranTypeDesRex = Pattern.compile("^[A-Z]{3}$");
                Pattern carTypeRex = Pattern.compile("^[\\s\\S]{2}型[\\s\\S]*$");

                if (disRex.matcher(d).matches()) {
                    vininfo.setDisplacement(d);
                } else if (disStandardRex.matcher(d).matches()) {
                    vininfo.setDisplacementStandard(d);
                } else if (tranTypeRex.matcher(d).matches()) {
                    vininfo.setTransmissionType(d);
                    if (StringUtils.equals("手动", d)) {
                        vininfo.setTransmissionDesc("手动变速器(MT)");
                    } else {
                        vininfo.setTransmissionDesc("自动变速器(AT)");
                    }
                } else if (tranTypeDesRex.matcher(d).matches()) {
                    vininfo.setTransmissionType("自动");
                    if (StringUtils.equals("CVT", d)) {
                        vininfo.setTransmissionDesc("无级变速器(CVT)");
                    } else if (StringUtils.equals("DCT", d)) {
                        vininfo.setTransmissionDesc("双离合变速器(DCT)");
                    } else {
                        vininfo.setTransmissionDesc(d);
                    }
                } else if (carTypeRex.matcher(d).matches()) {
                    List<String> typeAndSeats = Splitter.on("型").splitToList(d);
                    String carType = typeAndSeats.get(0) + "型";
                    vininfo.setCarType(carType);
                    Pattern seatsRex = Pattern.compile("^[(（].座[\\s\\S]*$");
                    String seats = typeAndSeats.get(1);
                    if (seatsRex.matcher(seats).matches()) {
                        String seat = StringUtils.substring(seats, 1, 3);
                        vininfo.setSeats(seat);
                    }
                }

            });
        }
    }
}
